package com.cyandpos.pad.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.OrderNoData;
import com.cyandpos.pad.sale.presenter.SalePresenterImpl;
import com.cyandpos.pad.utils.ToastUtils;
import com.google.gson.JsonObject;

import butterknife.BindView;

public class DepositActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.deposit_insent)
    Button depositinsent;
    @BindView(R.id.deposit_select)
    Button depositselect;
   @BindView(R.id.deposit_cancle)
    Button depositcancle;
   @BindView(R.id.tv_dialog_title_name)
    TextView tv_title_name;

    private ProgressDialog mProgressDialog;//等待框


    @Override
    protected void initData() {
        depositinsent.setOnClickListener(this);
        depositselect.setOnClickListener(this);
        depositcancle.setOnClickListener(this);
        tv_title_name.setOnClickListener(this);
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCancelable(false);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_deposit;
    }

    @Override
    public void onClick(View v) {
          switch (v.getId()){
              case R.id.deposit_insent:
                  if(ErpCommonData.ticketInfo.getTicketGoodCount()>0){
                      ToastUtils.show("请先点击订金单,然后再录入商品!");
                  }else{
//                      ErpCommonData.isDJD = true;//生成订金单
//                      ErpCommonData.isFSTH=false;
                      ErpCommonData.ticketInfo.setDJD(true);
                      ErpCommonData.ticketInfo.setFSTH(false);

                      mProgressDialog.setMessage("更新单号信息中……");
                      mProgressDialog.show();
                      PosHttpManager.CreateSaleId("","","", new OkHttpUtils.ResultCallback() {
                          @Override
                          public void onSuccess(JsonObject response) {
                              BaseResponse<OrderNoData> baseResponse = BaseResponse.fromJson(response.toString(),OrderNoData.class);
                              mProgressDialog.dismiss();
                              if(baseResponse.getCode().equals("0")){
                                  String orderNo = baseResponse.getData().getOrderNo();
                                  ErpCommonData.orderNo = orderNo;
                                  ErpCommonData.ticketInfo.setOrderNo(orderNo);
                                  finish();
                              }else {
                                  if (TextUtils.isEmpty(baseResponse.getTips())){
                                      ToastUtils.show(baseResponse.getMsg());
                                  }else {
                                      ToastUtils.show(baseResponse.getTips());
                                  }
                                  ErpCommonData.ticketInfo.setDJD(false);
                                  ErpCommonData.ticketInfo.setFSTH(false);
//                                  ErpCommonData.isDJD = false;
//                                  ErpCommonData.isFSTH=false;
                                  finish();
                              }
                          }

                          @Override
                          public void onFailure(String msg) {
                              mProgressDialog.dismiss();
                              ToastUtils.show(msg);
                              ErpCommonData.ticketInfo.setDJD(false);
                              ErpCommonData.ticketInfo.setFSTH(false);
//                              ErpCommonData.isDJD = false;
//                              ErpCommonData.isFSTH=false;
                              finish();
                          }
                      });
                  }
                  break;
              case R.id.deposit_select:
                  break;
              case R.id.deposit_cancle:
                  break;
              case R.id.tv_dialog_title_name:
                   finish();
                  break;
                  default:break;
          }
    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_title_name.setText("订金单功能选择");
    }
}
